package logic

import (
	"context"
	"errors"
	"zf/models/mysql"

	"zf/zfrpc/internal/svc"
	"zf/zfrpc/zfrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderAddLogic {
	return &OrderAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderAddLogic) OrderAdd(in *zfrpc.OrderAddRequest) (*zfrpc.OrderAddResponse, error) {
	// todo: add your logic here and delete this line

	order := mysql.Order{
		UserName:     in.UserName,
		UserMobile:   in.UserMobile,
		Address:      in.Address,
		GoodHomeName: in.GoodHomeName,
		GoodName:     in.GoodName,
		Num:          int(in.Num),
		Info:         in.Info,
		Price:        float64(in.Price),
	}
	if in.Num > 10 {
		return nil, errors.New("不能超过10份")
	}

	mysql.DB.Debug().Create(&order)

	return &zfrpc.OrderAddResponse{
		OrderId: int64(order.ID),
	}, nil
}
